﻿//--------------------------------------------------------------------------
// <copyright file="Proxy.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
// <summary>Proxy</summary>
//--------------------------------------------------------------------------
namespace MS.Msn.MediaSolutions.Common.Web
{
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Net;
    using System.IO;

    /// <summary>
    /// web proxy
    /// </summary>
    public static class Proxy
    {
        /// <summary>
        /// get request with credentials
        /// </summary>
        /// <param name="u">target URI</param>
        /// <param name="crd">credentials</param>
        /// <returns>http response text</returns>
        public static string GetRequest(string u, ICredentials crd)
        {
            string rtVal = string.Empty;

            WebClient wc = new WebClient();
            wc.Credentials = crd;

            rtVal = wc.DownloadString(u);
            return rtVal;
        }

        /// <summary>
        /// HTTP get method
        /// </summary>
        /// <param name="u">target url</param>
        /// <returns>response text</returns>
        public static string GetRequest(string u)
        {
            return GetRequest(u, CredentialCache.DefaultCredentials);
        }

        /// <summary>
        /// post HTTP request
        /// </summary>
        /// <param name="u">target url</param>
        /// <param name="requestString">request string</param>
        /// <param name="crd">credentials</param>
        /// <returns>http response text</returns>
        public static string PostRequest(string u, string requestString, ICredentials crd)
        {
            string rtVal = string.Empty;

            Uri reu = new Uri(u);
            WebRequest wrq = WebRequest.Create(reu);
            byte[] ba = Encoding.UTF8.GetBytes(requestString);
            wrq.Credentials = crd;
            wrq.Method = "POST";
            wrq.ContentType = "application/x-www-form-urlencoded";
            wrq.ContentLength = ba.Length;
            wrq.Timeout = 300000;

            using (Stream stm = wrq.GetRequestStream())
            {
                stm.Write(ba, 0, ba.Length);
            }

            using (WebResponse wres = wrq.GetResponse())
            {
                using (Stream stm = wres.GetResponseStream())
                {
                    using (StreamReader sr = new StreamReader(stm))
                    {
                        rtVal = sr.ReadToEnd();
                    }
                }
            }

            return rtVal;
        }

        /// <summary>
        /// HTTP post method
        /// </summary>
        /// <param name="u">target url</param>
        /// <param name="requestString">request string</param>
        /// <returns>response text</returns>
        public static string PostRequest(string u, string requestString)
        {
            return PostRequest(u, requestString, CredentialCache.DefaultCredentials);
        }

    }
}